package com.example.zsf.activity.mine;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.zsf.R;
import com.example.zsf.activity.mobile.MobileDetailActivity;
import com.example.zsf.adapter.FavoriteListAdapter;
import com.example.zsf.base.Presenter;
import com.example.zsf.base.Result;
import com.example.zsf.base.ZSFBaseActivity;
import com.example.zsf.model.FavoriteModel;
import com.example.zsf.model.MobileModel;
import com.example.zsf.presenter.MinePresenter;
import com.example.zsf.utils.DebouncedOnItemClickListener;
import com.example.zsf.view.NavigationBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyFavoriteActivity extends ZSFBaseActivity {

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;


    int curPage = 1;

    private FavoriteListAdapter favoriteListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_favorite);
        ButterKnife.bind(this);
        initUI();
        requestFavorite(curPage);
    }

    private void initUI() {
        Window window = getWindow();
        View decorView = window.getDecorView();
        int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        decorView.setSystemUiVisibility(option);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }


        navigationBar.setTitleText("我的关注");
        navigationBar.setLeftWhite();
        navigationBar.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        favoriteListAdapter = new FavoriteListAdapter(this, R.layout.item_favorite, null);
        recyclerView.setAdapter(favoriteListAdapter);
        favoriteListAdapter.setOnItemClickListener(new DebouncedOnItemClickListener(400){
            @Override
            public void onDebouncedItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                FavoriteModel.FavoriteListModel favoriteListModel = (FavoriteModel.FavoriteListModel) adapter.getItem(position);
                Intent intent = new Intent(MyFavoriteActivity.this, MobileDetailActivity.class);
                intent.putExtra("id",favoriteListModel.getAds_id());
                intent.putExtra("image_url",favoriteListModel.getAds_img());
                startActivity(intent);
            }
        });

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                curPage = 1;
                requestFavorite(curPage);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                curPage++;
                requestFavorite(curPage);
            }
        });

    }


    private void requestFavorite(int page) {
        disposed(MinePresenter.getMyFavorite(page, new Presenter.PresenterListener<Result<FavoriteModel>>() {
            @Override
            public void onSuccess(Result<FavoriteModel> object) {
                if (object.code.equals("1")) {
                    if (curPage == 1) {
                        favoriteListAdapter.setList(object.data.getFollow());
                        favoriteListAdapter.notifyDataSetChanged();
                        refreshLayout.finishRefresh();
                        return;
                    }
                    if (object.data.getFollow().size() > 0) {
                        favoriteListAdapter.addData(object.data.getFollow());
                        favoriteListAdapter.notifyDataSetChanged();
                        refreshLayout.finishLoadMore();
                    } else {
                        refreshLayout.finishLoadMore();
                        refreshLayout.setNoMoreData(true);
                        curPage--;
                    }
                } else {
                    ToastUtils.showShort(object.msg);
                    refreshLayout.finishRefresh(false);
                    refreshLayout.finishLoadMore(false);
                    curPage--;
                }
            }

            @Override
            public void onError(String error) {
                ToastUtils.showShort(error);
                refreshLayout.finishRefresh(false);
                refreshLayout.finishLoadMore(false);
                curPage--;
            }
        }));

    }

}